package com.ggy.babybed.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ggy.babybed.R;
import com.ggy.babybed.model.IModel;
import com.ggy.babybed.model.SwayModel;
import com.ggy.babybed.socketwork.command.SwayCommand;
import com.ggy.babybed.socketwork.util.NetworkUtil;
import com.ggy.babybed.wilddog.WildDogDataChange;

import java.util.Collection;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SwayFragment.OnFragmentInteractionListener} message
 * to handle interaction events.
 * Use the {@link SwayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SwayFragment extends Fragment implements View.OnClickListener,SeekBar.OnSeekBarChangeListener,WildDogDataChange {
    private static final String ARG_PARAM1 = "ID";
    private static final String ARG_PARAM2 = "name";
    private static final String ARG_PARAM3 = "password";

    public static final String TAG = "SwayFragmet";

    @Override
    public void onchange(Collection data) {

    }

    private SwayModel swayModel;

    private OnFragmentInteractionListener mListener;

    private Button button;
    private SeekBar sway_bar_fu;
    private SeekBar sway_bar_pin;
    private TextView text_fu;
    private TextView text_pin;
    private ImageView imageView;
    private Animation rotate;

    public SwayFragment() {
        swayModel = new SwayModel();
    }

    /**
     *
     * @param model Parameter 1.
     * @return A new instance of fragment SwayFragment.
     */
    public static SwayFragment newInstance(IModel model) {
        SwayFragment fragment = new SwayFragment();
        Bundle args = new Bundle();
        args.putLong(ARG_PARAM1, model.getID());
        args.putString(ARG_PARAM2, model.getName());
        args.putString(ARG_PARAM3, model.getPassword());
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            swayModel.setID(getArguments().getLong(ARG_PARAM1, 1l));
            swayModel.setName(getArguments().getString(ARG_PARAM2, "baby"));
            swayModel.setPassword(getArguments().getString(ARG_PARAM3, "123"));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sway, container, false);
        button = (Button) view.findViewById(R.id.sway_open);
        button.setOnClickListener(this);
        sway_bar_fu = (SeekBar) view.findViewById(R.id.sway_bar_fu);
        sway_bar_fu.setOnSeekBarChangeListener(this);
        sway_bar_pin = (SeekBar) view.findViewById(R.id.sway_bar_pin);
        sway_bar_pin.setOnSeekBarChangeListener(this);
        text_fu = (TextView) view.findViewById(R.id.txt_fu);
        text_pin = (TextView) view.findViewById(R.id.txt_pin);
        imageView = (ImageView) view.findViewById(R.id.sway_img);
        rotate = AnimationUtils.loadAnimation(this.getContext(), R.anim.sway_rotate);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        boolean tag=false;
        if(v.getId() == R.id.sway_open){
            if(NetworkUtil.isConnected(this.getContext())){

                if(button.getText().equals(getResources().getString(R.string.sway_bt_open))){
                    button.setText(R.string.sway_bt_close);
                    swayModel.setIsOpen(true);
                    //tag = CustomWilddog.getInstance(this.getContext()).setValue("led",1);
                }else{
                    button.setText(R.string.sway_bt_open);
                    swayModel.setIsOpen(false);
                    //tag = CustomWilddog.getInstance(this.getContext()).setValue("led",0);
                }
                SwayCommand swayCmd = new SwayCommand(swayModel);
                    if(tag){
                        Log.i(TAG, "success");
                    }else{
                        Log.e(TAG, "failed");
                    }
                    button.setText(R.string.sway_bt_open);
                    swayModel.setIsOpen(false);
                }
            }else{
                Toast.makeText(this.getContext(), R.string.net_not_open, Toast.LENGTH_SHORT);
            }

        }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(progress > 5) {
            if(seekBar.getId() == R.id.sway_bar_fu){
                swayModel.setRange(progress);
                text_fu.setText(getResources().getText(R.string.sway_text_fu)+""+progress);
            } else if(seekBar.getId() == R.id.sway_bar_pin){
                rotate.setDuration(100000 / progress);
                swayModel.setRate(progress);
                text_pin.setText(getResources().getText(R.string.sway_text_pin)+""+progress);
            }
            imageView.startAnimation(rotate);
        }else{
            rotate.cancel();
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    /**
     * This message must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
